package net.jeeshop.core.smscode.base.common;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.connection.ApacheHttpClient;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jsms.api.SendSMSResult;
import cn.jsms.api.ValidSMSResult;
import cn.jsms.api.common.SMSClient;
import cn.jsms.api.common.model.SMSPayload;
import net.jeeshop.core.smscode.constant.ConstantPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class JSMSExample {

    protected static final Logger LOG = LoggerFactory.getLogger(JSMSExample.class);

    public static String testSendSMSCode(String phone) {
        SMSClient client = new SMSClient(ConstantPool.MASTER_SECRET, ConstantPool.APP_KEY);
        SMSPayload payload = SMSPayload.newBuilder()
                .setMobildNumber(phone)
                .setTempId(1)
                .build();
        try {
            cn.jsms.api.SendSMSResult res = client.sendSMSCode(payload);
            System.out.println(res.toString());
            LOG.info(res.toString());
            return res.toString();
        } catch (APIConnectionException var5) {
            LOG.error("Connection error. Should retry later. ", var5);
        } catch (APIRequestException var6) {
            LOG.error("Error response from JPush server. Should review and fix it. ", var6);
            LOG.info("HTTP Status: " + var6.getStatus());
            LOG.info("Error Message: " + var6.getMessage());
        }
        return null;
    }

    public static String testSendValidSMSCode(String id, String code) {
        SMSClient client = new SMSClient(ConstantPool.MASTER_SECRET, ConstantPool.APP_KEY);
        try {
            ValidSMSResult res = client.sendValidSMSCode(id, code);
            System.out.println(res.toString());
            return res.toString();
        } catch (APIConnectionException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            e.printStackTrace();
            if (e.getErrorCode() == 50010) {
                // do something
            }
            System.out.println(e.getStatus() + " errorCode: " + e.getErrorCode() + " " + e.getErrorMessage());
        }
        return null;
    }

    /**
     *  The default value of ttl is 60 seconds.
     */
//    public static void testSendVoiceSMSCode(String phone) {
//        SMSClient client = new SMSClient(ConstantPool.MASTER_SECRET, ConstantPool.APP_KEY);
//        SMSPayload payload = SMSPayload.newBuilder()
//                .setMobildNumber(phone)
//                .setTTL(90)
//                .build();
//        try {
//            SendSMSResult res = client.sendVoiceSMSCode(payload);
//            LOG.info(res.toString());
//        } catch (APIRequestException e) {
//            LOG.error("Error response from JPush server. Should review and fix it. ", e);
//            LOG.info("HTTP Status: " + e.getStatus());
//            LOG.info("Error Message: " + e.getMessage());
//        } catch (APIConnectionException e) {
//            LOG.error("Connection error. Should retry later. ", e);
//        }
//    }
//
//    public static void testSendTemplateSMS() {
//        SMSClient client = new SMSClient(masterSecret, appkey);
//        SMSPayload payload = SMSPayload.newBuilder()
//                .setMobildNumber("13800138000")
//                .setTempId(1)
//                .addTempPara("test", "jpush")
//                .build();
//        try {
//            SendSMSResult res = client.sendTemplateSMS(payload);
//            LOG.info(res.toString());
//        } catch (APIRequestException e) {
//            LOG.error("Error response from JPush server. Should review and fix it. ", e);
//            LOG.info("HTTP Status: " + e.getStatus());
//            LOG.info("Error Message: " + e.getMessage());
//        } catch (APIConnectionException e) {
//            LOG.error("Connection error. Should retry later. ", e);
//        }
//    }

}